package com.shuai.shuaisecondhand_springboot.mapper;


import com.shuai.shuaisecondhand_springboot.entity.Admin;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminMapper {

    //sql->登录查询
    @Select("SELECT * FROM admin WHERE username=#{username} AND password=#{password}")
    Admin login(@Param("username") String username, @Param("password") String password);

    //sql->注册
    @Insert("INSERT INTO admin(username,password) VALUES (#{username},#{password})")
    Boolean registerAdmin(@Param("username") String username,@Param("password") String password);

    //sql->查询
    @Select("SELECT * FROM admin")
    List<Admin> findAll();

    //sql->根据用户名查询id
    @Select("SELECT id FROM admin WHERE username=#{username}")
    String findId(@Param("username") String username);

    //sql->根据id查询所有数据
    @Select("SELECT * FROM admin WHERE id=#{id}")
    Admin findOne(@Param("id") Integer id);

    //sql->插入
    @Insert("INSERT INTO admin(username,phone) VALUES (#{username},#{phone})")
    Boolean insertAdmin(Admin admin);

    //mybatis的xml方式用sql->修改
    @Update("UPDATE admin SET username=#{username},avatar=#{avatar},phone=#{phone} WHERE id=#{id}")
    Boolean updateAdmin(Admin admin);

    //sql->修改个人密码
    @Update("UPDATE admin SET password=#{newpassword} WHERE password=#{password} AND id=#{id}")
    Boolean updatePassword(@Param("id") Integer id,@Param("password") String password,@Param("newpassword") String newpassword);

    //sql->删除
    @Delete("DELETE FROM admin WHERE id=#{id}")
    Integer deleteAdmin(long id);

    //mybatis的xml方式用sql->批量删除
    Integer deleteAdminBatch(List<Integer> ids);

    //sql->分页查询
    @Select("SELECT *  FROM admin WHERE (IFNULL(username,'') LIKE concat('%', #{username}, '%')) limit #{pageNum}, #{pageSize}")
    List<Admin> getAdminPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize,@Param("username") String username);

    //sql->根据用户名查询所有数据量
    @Select("SELECT count(*) FROM admin WHERE (IFNULL(username,'') LIKE concat('%', #{username}, '%'))")
    int getTotal(@Param("username") String username);

    //sql->查询管理员总数
    @Select("SELECT COUNT(*) FROM admin")
    int getAllTotal();
}
